package com.google.gwt.mobile.phonegap.api;

import com.google.gwt.core.client.JavaScriptObject;

/**
 * The Class Camera.
 */
public final class Camera {

  /**
   * Gets the picture.
   * 
   * @param callback the callback
   * @return the picture
   */
  public static void getPicture(final Callback callback) {
    getPicture(callback, (JavaScriptObject) null);
  }

  /**
   * Gets the picture.
   * 
   * @param callback the callback
   * @param options the options
   * @return the picture
   */
  public static void getPicture(final Callback callback, final Options options) {
    getPicture(callback, options.getOptions());
  }

  private native static void getPicture(Callback callback, JavaScriptObject options) /*-{
		$wnd.navigator.camera
				.getPicture(
						function(imageData) {
							callback.@com.google.gwt.mobile.phonegap.api.Camera.Callback::onSuccess(Ljava/lang/String;)(imageData);
						},
						function(message) {
							callback.@com.google.gwt.mobile.phonegap.api.Camera.Callback::onError(Ljava/lang/String;)(message);
						}, options);
  }-*/;

  /**
   * The Interface Callback.
   */
  public interface Callback {
    void onSuccess(String imageData);

    void onError(String message);
  }

  /**
   * The Enum DestinationType.
   */
  public enum DestinationType {
    DATA_URL, FILE_URI
  }

  /**
   * The Enum SourceType.
   */
  public enum SourceType {
    PHOTOLIBRARY, CAMERA, SAVEDPHOTOALBUM
  }

  /**
   * The Class Options.
   */
  public static class Options {
    Options self = this;
    JavaScriptObject options = JavaScriptObject.createObject();

    /**
     * Quality.
     * 
     * @param q the q
     * @return the options
     */
    public native Options quality(int q) /*-{
			this.@com.google.gwt.mobile.phonegap.api.Camera.Options::options.quality = q;
			return this.@com.google.gwt.mobile.phonegap.api.Camera.Options::self;
    }-*/;

    // TODO: is there way to pass enum to javascript without using this method?
    /**
     * Destination type.
     * 
     * @param d the d
     * @return the options
     */
    public Options destinationType(final DestinationType d) {
      return destinationType(d.ordinal());
    }

    private native Options destinationType(int d) /*-{
			this.@com.google.gwt.mobile.phonegap.api.Camera.Options::options.destinationType = d;
			return this.@com.google.gwt.mobile.phonegap.api.Camera.Options::self;
    }-*/;

    /**
     * Source type.
     * 
     * @param s the s
     * @return the options
     */
    public Options sourceType(final SourceType s) {
      return sourceType(s.ordinal());
    }

    private native Options sourceType(int s) /*-{
			this.@com.google.gwt.mobile.phonegap.api.Camera.Options::options.sourceType = s;
			return this.@com.google.gwt.mobile.phonegap.api.Camera.Options::self;
    }-*/;

    /**
     * Allow edit.
     * 
     * @param b the b
     * @return the options
     */
    public native Options allowEdit(boolean b) /*-{
			this.@com.google.gwt.mobile.phonegap.api.Camera.Options::options.allowEdit = b;
			return this.@com.google.gwt.mobile.phonegap.api.Camera.Options::self;
    }-*/;

    private JavaScriptObject getOptions() {
      return options;
    }
  }
}
